﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using LinkCard.Utils;
using LinkCard.Dao;

namespace LinkCard.ShopCP.User
{
    public partial class user_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (Convert.ToBoolean(Session["SHOPADMIN"]) == false)
                {
                    Response.Redirect("/message_box.aspx?msg=5");
                }
            }

            if (!Page.IsPostBack)
            {
                DataBindPaging();
            }
        }

        private void DataBindPaging()
        {

            try
            {
                string sSQL = "SELECT * FROM [ShopStaff] WHERE Type='NORMAL' and SupplierID = " + 
                                Convert.ToInt64(Session["USER"].ToString()) + " order by UserName";
                DataTable dt = DataHelper.SelectTable(sSQL, false);


                PagedDataSource pageds = new PagedDataSource();
                pageds.DataSource = dt.DefaultView;
                pageds.AllowPaging = true;
                pageds.PageSize = 20;

                int curpage = 0;

                if (Request.QueryString["page"] != null)
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    curpage = 1;
                }

                pageds.CurrentPageIndex = curpage - 1;

                if (pageds.PageCount < 2) divPager.Visible = false;

                PagerShow.Text = hash1way.ShowPageNavigation(curpage, pageds.PageCount);

                msgList.DataSource = pageds;
                msgList.DataBind();
                msgNoData.Visible = false;
                if (msgList.Items.Count == 0) { msgNoData.Visible = true; }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void msgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView row = (DataRowView)e.Item.DataItem;

            var chkDelete = (HtmlInputCheckBox)e.Item.FindControl("chkDelete");
            chkDelete.Value = row["UserID"].ToString();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string sArray = "(0";
            string sDaily = "0";

            foreach (RepeaterItem item in msgList.Items)
            {
                var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                if (chkDelete.Checked)
                {
                    sArray = sArray + "," + chkDelete.Value + "";
                    //sDaily = sDaily + "," + chkDelete.Value + ""; 
                    //Lưu log
                    var logAction = LogActions.LogItem(0, Convert.ToInt64(Session["USER"].ToString()), 0, 0, Convert.ToInt64(chkDelete.Value), "Xoá Đại lý " + sDaily,"Delete", "ShopCP");
                }
            }
            sArray = sArray + ")";

            string sSQL = "delete [ShopStaff] where UserID in" + sArray;
            DataHelper.ExecuteNonQuery(sSQL, true);

            Response.Redirect("/User/user_list.aspx");
        }
    }
}